package com.ai.controller;

import com.ai.config.AiConfig;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.service.TokenStream;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
public class OtherAIController {
    @Autowired
    AiConfig.Assistant assistant;

    @Autowired
    StreamingChatLanguageModel qwenStreamingChatModel;

    @Operation(summary = "对话", description = "对话")
    @GetMapping(value = "/memory_chat")
    public String memoryChat(@RequestParam String message) {
        return assistant.chat(message);
    }


    @Operation(summary = "流式对话", description = "流式对话")
    @GetMapping(value = "/memory_stream_chat", produces = "text/stream;charset=UTF-8")
    public Flux<String> memoryStreamChat(@RequestParam String message, HttpServletResponse response) {
        TokenStream stream = assistant.stream(message);

        return Flux.create(sink -> {
            stream.onPartialResponse(s -> sink.next(s))
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();

        });
    }


}